package com.bysc.executor.handler;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bysc.commonutils.vo.CmsActivity;
import com.bysc.executor.client.CmsClient;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static com.bysc.commonutils.enums.ActivityEnum.*;

@Component
public class ActivityJobHandler {

    @Autowired
    private CmsClient cmsClient;

    @XxlJob("ActivityStatusJob")
    public void ActivityStatusJob() throws Exception {
        try {
            System.out.println("ActivityStatusJob执行了.... " + new Date());

            // 获取活动列表
            List<CmsActivity> list = JSON.parseObject(
                    JSON.toJSONString(cmsClient.getAllActivityList().getData().get("list")),
                    new TypeReference<List<CmsActivity>>() {}
            );

            if (list != null) {
                Date currentDateTime = new Date(System.currentTimeMillis());
                System.out.println("当前时间：" + DateUtil.format(currentDateTime, "yyyy-MM-dd HH:mm:ss"));
                for (CmsActivity record : list) {
                    if (record == null) {
                        continue;
                    }

                    System.out.println("更新前：" + record);

                    // 获取活动的时间字段
                    Date joinStart = DateUtil.parse(record.getJoinStart(),"yyyy-MM-dd HH:mm:ss");
                    Date joinEnd = DateUtil.parse(record.getJoinEnd(),"yyyy-MM-dd HH:mm:ss");
                    Date activityStart = DateUtil.parse(record.getActivityStart(),"yyyy-MM-dd HH:mm:ss");
                    Date activityEnd = DateUtil.parse(record.getActivityEnd(),"yyyy-MM-dd HH:mm:ss");
                    // 根据活动时间设置状态
                    if (joinStart != null && joinStart.after(currentDateTime)) { // 活动未开始
                        record.setStatus(NOT_START.getId());
                    } else if (joinStart != null && joinStart.before(currentDateTime) &&
                            joinEnd != null && joinEnd.after(currentDateTime)) { // 活动报名中
                        record.setStatus(JOIN_START.getId());
                    } else if (activityStart != null && activityStart.before(currentDateTime) &&
                            activityEnd != null && activityEnd.after(currentDateTime)) { // 活动进行中
                        record.setStatus(ONGOING.getId());
                    } else { // 活动已结束
                        record.setStatus(ENDED.getId());
                    }

                    System.out.println("更新后：" + record);
                    cmsClient.updateActivity(record);
                }

            }
            System.out.println("ActivityStatusJob执行结束了.... " + new Date());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
//http://10.64.17.94:9999
